package utils;

import java.util.List;
import java.util.regex.Pattern;
import android.app.Instrumentation;
import android.support.test.uiautomator.By;
import android.support.test.uiautomator.UiDevice;
import android.support.test.uiautomator.UiObject2;
import android.util.Log;

public class UaSearchText {
	private static final String TAG="yiqijiao";
	/**
	 * 封装uiautomator2.0界面文本搜索方法，适用于跨进程操作：如通知栏消息测试
	 * @param text
	 * @param ins
	 * @return
	 */
	public static boolean searchText(String text,Instrumentation ins){
		UiDevice uiDevice = UiDevice.getInstance(ins);
		List<UiObject2> list;
		boolean findText=false;
		long endTime=System.currentTimeMillis()+5000;
		do {
			list = uiDevice.findObjects(By.text(text));
			if (list.size() != 0) {
				findText=true;
				break;
			}else{
				list = uiDevice.findObjects(By.text(Pattern.compile(text)));
				if (list.size() != 0) {
					findText=true;
					break;
				} 
			}
			if(System.currentTimeMillis()>endTime){
				Log.e(TAG, "未搜索到文本");
				break;
			}
		} while (true);
		return findText;
	}
}
